package com.mune.gulimall.seckill.controller;

import com.mune.common.utils.R;
import com.mune.gulimall.seckill.service.SeckillService;
import com.mune.gulimall.seckill.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillControler {
  @Autowired
  SeckillService seckillService;

  @GetMapping("/currentSeckillSkus")
  @ResponseBody
  public R getCurrentSeckillSkus() {
    List<SecKillSkuRedisTo> skus = seckillService.getCurrentSeckillSkus();
    return R.ok().setData(skus);
  }

  @GetMapping("/sku/seckill/{skuId}")
  @ResponseBody
  public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
    SecKillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
    return R.ok().setData(to);
  }

  @GetMapping("/kill")
  public String secKill(@RequestParam("killId") String killId,
                        @RequestParam("code") String code,
                        @RequestParam("num") Integer num,
                        Model model) {
    String orderSn = seckillService.kill(killId, code, num);
    model.addAttribute("orderSn", orderSn);
    return "success";
  }
}
